package com.tykboot.controller;

import com.tykboot.core.base.BaseController;
import com.tykboot.core.persistence.Criteria;
import com.tykboot.core.persistence.Restrictions;
import com.tykboot.entity.TbProduct;
import com.tykboot.message.MsgCode;
import com.tykboot.message.ResponseMsg;
import com.tykboot.service.ITbProductService;
import com.tykboot.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  Controller
 *
 * @author LiuYu
 */ 
@Api(tags = "product", description = "/api/products")
@RestController
@RequestMapping(BaseController.API + "/products")
public class TbProductController extends BaseController {

	@Autowired
	private ITbProductService tbProductService;

	@ApiOperation(value = "Add an product", notes = "Add an product")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true)
	})
	@PostMapping("")
	public ResponseMsg<TbProduct> add(@RequestHeader("financialId") String financialId,
									  @RequestBody ProductVo requestBody) {
		ResponseMsg<TbProduct> responseMsg = new ResponseMsg<>();
		TbProduct entity = new TbProduct();
		BeanUtils.copyProperties(requestBody, entity);
		entity.setFinancialId(financialId);
		entity = tbProductService.save(entity);
		responseMsg.setData(entity);
		return responseMsg;
	}

	@ApiOperation(value = "Update an product", notes = "Update an product")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true),
			@ApiImplicitParam(name = "productId", value = "productId", dataType = "string", paramType = "path", required = true)
	})
	@PutMapping("/{productId}")
	public ResponseMsg<TbProduct> update(@RequestHeader("financialId") String financialId,
										 @PathVariable("productId") String productId,
										 @RequestBody ProductVo requestBody) {
		ResponseMsg<TbProduct> responseMsg = new ResponseMsg<>();
		TbProduct entity = tbProductService.findById(productId);
		if (entity == null) {
			return new ResponseMsg<>(MsgCode.Param_Error);
		}
		BeanUtils.copyProperties(requestBody, entity, "productId");
		entity.setFinancialId(financialId);
		entity = tbProductService.save(entity);
		responseMsg.setData(entity);
		return responseMsg;
	}

	@ApiOperation(value = "Get Products", notes = "Get Products")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true)
	})
	@GetMapping("")
	public ResponseMsg<List<TbProduct>> queryProducts(@RequestHeader("financialId") String financialId) {
		ResponseMsg<List<TbProduct>> responseMsg = new ResponseMsg<>();
		Criteria<TbProduct> req = new Criteria<>();
		req.add(Restrictions.eq("financialId", financialId));
		List<TbProduct> list = tbProductService.findAll(req);
		responseMsg.setData(list);
		return responseMsg;
	}

	@ApiOperation(value = "Get account Product", notes = "Get Product related to an account")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true),
			@ApiImplicitParam(name = "accountId", value = "accountId", dataType = "string", paramType = "path", required = true)
	})
	@GetMapping("/{accountId}")
	public ResponseMsg<List<TbProduct>> queryProductById(@RequestHeader("financialId") String financialId,
														 @PathVariable("accountId") String accountId) {
		ResponseMsg<List<TbProduct>> responseMsg = new ResponseMsg<>();
		Criteria<TbProduct> req = new Criteria<>();
		req.add(Restrictions.eq("accountId", accountId));
		req.add(Restrictions.eq("financialId", financialId));
		List<TbProduct> list = tbProductService.findAll(req);
		responseMsg.setData(list);
		return responseMsg;
	}

}
